package www.game.pay.user;
import www.game.pay.demo;

import java.sql.*;
import java.util.Scanner;

public class User {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("用户登录平台");
            System.out.println("1.注册新用户");
            System.out.println("2.查看所有用户");
            System.out.println("3.用户登录");
            System.out.println("4.退出系统");
            System.out.println("请选择操作：");
            int choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1:
                    registerUser();
                    break;
                case 2:
                        showUser();
                    break;
                case 3:
                            UserLogin();
                    break;
                case 4:
                    System.out.println("谢谢使用");
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }
    //注册用户的方法
    public static void registerUser() {
        System.out.println("用户注册");
        System.out.print("请输入用户名: ");
        String username = sc.nextLine();
        System.out.print("请输入密码: ");
        String password = sc.nextLine();
        System.out.print("请输入邮箱: ");
        String email = sc.nextLine();

        // 默认新注册用户不是管理员
        int isAdmin = 0;

        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = demo.getConnection();
            String sql = "INSERT INTO users (username, password, email, is_admin) VALUES (?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, email);
            pstmt.setInt(4, isAdmin);  // 设置管理员权限

            int result = pstmt.executeUpdate();
            if (result > 0) {
                System.out.println("用户注册成功！");
            } else {
                System.out.println("用户注册失败");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            // 关闭资源
            try {
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                System.out.println("关闭连接时出错");
            }
        }
    }

//    显示所有用户
    public static void showUser(){
        String sql = "select * from users";

        try{
            Connection conn = null;
            PreparedStatement pstmt = null;
            conn = demo.getConnection();
            pstmt=conn.prepareStatement(sql);
//            executeQuery只用于查询  Resultset 就像一张表格包含读取的数据表
            ResultSet result =pstmt.executeQuery();
            System.out.println("用户列表");
           while(result.next()){
                int id =result.getInt("id");
                String username = result.getString("username");
                String email = result.getString("email");
                System.out.println("ID:"+id+",用户名:"+username+",电子邮件:"+email);

           }
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

//    用户登录
   public static void UserLogin(){
            System.out.println("请输入你的用户名");
            String username= sc.nextLine();
//            sc.nextLine();
            System.out.println("请输入你的密码");
            String password = sc.nextLine();
            String sql = "select * from users where username=? and password=? ";
            try{
            Connection conn = null;
            PreparedStatement pstmt = null;
            conn = demo.getConnection();
            pstmt=conn.prepareStatement(sql);
//            设置参数
            pstmt.setString(1,username);
            pstmt.setString(2,password);

            ResultSet resultSet=pstmt.executeQuery();
            if (resultSet.next()){
                String loginUsername=resultSet.getString("username");
                int userId = resultSet.getInt("id");
                int isAdmin = resultSet.getInt("is_admin");
                System.out.println("登录成功!"+loginUsername);
                userMenu(username,userId);
            }
            else{
                System.out.println("用户id或密码错误");
            }

                pstmt.close();
                conn.close();
            }catch (SQLException e){
                System.out.println(e.getMessage());
            }
   }

   public static void userMenu(String username, int userId){
       System.out.println("欢迎您，亲爱的"+username+"用户");
        while(true){

            System.out.println("用户菜单");
            System.out.println("1.用户系统");
            System.out.println("2.游戏商店");
            System.out.println("3.游戏库");
            System.out.println("4.愿望单");
            System.out.println("5.好友系统");
            System.out.print("请选择：");
            int choice = sc.nextInt();
            sc.nextLine();
            switch (choice){
                case 1:System.out.println("暂无游戏商店功能");
                usershop(userId);
                break;
                case 2:System.out.println("暂无游戏库功能");
                wishswareshouse(userId);
                break;
                case 3:System.out.println("暂无好友功能");
                    showFriends(userId);
                break;
                case 4:
                    System.out.println("退出登录");
                   return;
                   default:
                        System.out.println("无效选择");
            }
        }
   }
   //游戏商城
   // 用户愿望单- 查看用户已购买的游戏
   public static void usershop(int userId) {
       // 正确的SQL语句，使用参数化查询
       String sql = "SELECT g.* FROM games g " +
               "JOIN user_library ul ON g.id = ul.game_id " +
               "WHERE ul.user_id = ?";

       try {
           Connection conn = demo.getConnection();
           PreparedStatement pstmt = conn.prepareStatement(sql);
           pstmt.setInt(1, userId); // 设置用户ID参数

           ResultSet rs = pstmt.executeQuery();

           System.out.println("=== 我的游戏库 ===");
           System.out.println("您拥有的游戏：");
           System.out.println("----------------------------------------");

           boolean hasGames = false;
           while (rs.next()) {
               hasGames = true;
               int gameId = rs.getInt("id");
               String title = rs.getString("title");
               String description = rs.getString("description");
               double price = rs.getDouble("price");
               String publisher = rs.getString("publisher");

               System.out.println("游戏ID: " + gameId);
               System.out.println("游戏名称: " + title);
               System.out.println("描述: " + description);
               System.out.println("价格: " + price);
               System.out.println("出版商: " + publisher);
               System.out.println("----------------------------------------");
           }

           if (!hasGames) {
               System.out.println("您还没有购买任何游戏。");
               System.out.println("快去游戏商店看看吧！");
           }

           rs.close();
           pstmt.close();
           conn.close();

       } catch (SQLException e) {
           System.out.println("查询游戏库失败：" + e.getMessage());
           e.printStackTrace();
       }
   }

    public static void wishswareshouse(int userId) {
        // SQL查询：获取用户愿望单中的游戏
        String sql = "SELECT games.id, games.title, games.price " +
                "FROM wishlist " +
                "JOIN games ON wishlist.game_id = games.id " +
                "WHERE wishlist.user_id = ?";

        try {
            Connection conn = demo.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            ResultSet rs = pstmt.executeQuery();

            System.out.println("=== 我的愿望单 ===");

            // 检查是否有游戏
            boolean hasGames = false;
            while (rs.next()) {
                hasGames = true;
                int gameId = rs.getInt("id");
                String title = rs.getString("title");
                double price = rs.getDouble("price");

                System.out.println("游戏: " + title + " | 价格: ¥" + price + " | ID: " + gameId);
            }

            if (!hasGames) {
                System.out.println("愿望单为空");
            }

            // 关闭连接
            rs.close();
            pstmt.close();
            conn.close();

        } catch (SQLException e) {
            System.out.println("查询失败: " + e.getMessage());
        }



    }
    public static void showFriends(int userId) {
        // SQL：查找我的好友
        String sql = "SELECT users.username, friends.friend_note " +
                "FROM friends " +
                "JOIN users ON friends.friend_id = users.id " +
                "WHERE friends.user_id = ?";

        try {
            Connection conn = demo.getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            ResultSet rs = pstmt.executeQuery();

            System.out.println("我的好友列表:");

            boolean found = false;
            while (rs.next()) {
                found = true;
                String friendName = rs.getString("username");
                String note = rs.getString("friend_note");

                System.out.println(friendName + " (" + note + ")");
            }

            if (!found) {
                System.out.println("暂无好友");
            }

            rs.close();
            pstmt.close();
            conn.close();

        } catch (SQLException e) {
            System.out.println("查询失败: " + e.getMessage());
        }
    }

}





